from myelin.Ns import MYELINS
from nodetypes.formats.mml.Profiler import AgentProfiler
from nodetypes.models.Document import Document

from Commit import Form


class Form(Form):
    
    newProfiler = AgentProfiler
    
    newDocumentModel = Document.Retrieve
    newDocumentDriver = staticmethod(Document.Initialize)
    
    def createInstance(self, context, node):
        eventData = context.event.data  
        
        params = eventData.params
        results = eventData.results
        
        nxa = node.xml_append
        nxc = node.xml_create_element
        
        super(Form,self).createInstance(
            context,
            nxa(
                nxc(
                    u'a:search-params', MYELINS
                )
            )
        )        
        
        header = eventData.resultsHeader
        if header.size:
            self.createResults(
                context,
                nxa(
                    nxc(
                        u'a:search-results', MYELINS,
                        attributes = {
                            u'total'        : unicode(header.estimate),
                            u'count'        : unicode(params['count']),
                            u'start'        : unicode(eventData.searchBaseIndex + 1),
                            u'end'          : unicode(eventData.searchBaseIndex + header.size),
                            u'page'         : unicode(params['page']),
                        }
                    )
                ),
                results,
                params,
                eventData
            )
        else:
            nxa(
                nxc(
                    u'a:search-results', MYELINS
                )
            )
        
        return node
    
    
    def createResults(self, context, node, results, params, eventData):
        
        root = context.defaults.document_root
        map = context.uriMapper.apply
        
        nxa = node.xml_append
        nxc = node.xml_create_element
        
        #locale = eventData.searchLocale or \
        #    context.preferences['locale']
        locale = context.preferences['locale']
        
        # FIX ME
        prof = self.newProfiler(root, locale)
        cap = prof.createProfile        
        
        F = context.repo.fetchResource
        newDocModel = self.newDocumentModel
        newDocDriver = self.newDocumentDriver
        uriScopeResolver = context.uriScopeResolver
        
        for r in results:
            n = F(r.path)
            d = n.getAbsolutePathAsUri()
            n = newDocModel(
                newDocDriver(
                    n.getModel().driver,
                    uriScopeResolver = uriScopeResolver
                ),
                n.uri,
                d,d
            )
            n_join = n.rdf_join
            cur = nxa(
                nxc(
                    u'a:node', MYELINS,
                    attributes = {
                        u'key'      : u'all',
                        u'node-id'  : n.rdf_value,
                        u'tag'      : n_join('mTag'),
                        u'created'  : n_join('dctermsCreated'),                        
                        u'uri'     : map(d),
                        u'percent' : unicode(r.percent),
                    }
                )
            )
            
            cxa = cur.xml_append
            cxc = cur.xml_create_element
            
            cxa(
                cxc(
                    u'a:title', MYELINS,
                    content = n_join('dcTitle')
                )
            )
            cxa(
                cxc(
                    u'a:language', MYELINS,
                    content = locale
                )
            )
            cxa(
                cxc(
                    u'a:description', MYELINS,
                    content = n_join('dcDescription')
                )
            )
            
            for a in n.dcCreator:
                cap(
                    context,
                    cur,
                    a,
                    u'a:creator',
                    MYELINS
                )
            
            a = n_join('dctermsModified')
            if a:
                cur.modified = a
                for a in n.mLast_Modifier:
                    cap(
                        context,
                        cur,
                        a,
                        u'a:last-modifier',
                        MYELINS
                    )
            
        return node